In [1]:
-- Write to HDF5 using deepmind/torch-hdf5
require 'hdf5'
local myFile = hdf5.open('/tmp/sample_torch.h5', 'w')
myFile:write('random', torch.rand(5, 5))
myFile:write('tensor', torch.Tensor({{1,2,3,4,5},{6,7,8,9,10}}))
myFile:close()
In [4]:
-- Read from HDF5
require 'hdf5'
local myFile = hdf5.open('/tmp/sample_torch.h5', 'r')
local random = myFile:read('random'):all()
tensor = myFile:read('tensor'):all()
myFile:close()
print('Random:',random)
print('Tensor:',tensor)
Out[4]:
In [2]:
print({{3}})
Out[2]:
In [14]:
-- Create tensors
a = torch.ones(1,5,2)
b = torch.zeros(1,1,4):float()
c = torch.Tensor(1,1):fill(4)
print(a,b,c)
print(a:transpose(2,3))
print(a[1][{{2,3}}])
print('A size:',a:size())
Out[14]:
In [ ]:
1.- Load data
2.- Define and train the network
3.- Test network over new data
based on: https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning%20with%20Torch.ipynb
In [1]:
require 'paths'
if (not paths.filep("cifar10torchsmall.zip")) then
os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')
os.execute('unzip cifar10torchsmall.zip')
end
trainset = torch.load('cifar10-train.t7')
testset = torch.load('cifar10-test.t7')
classes = {'airplane', 'automobile', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck'}
In [2]:
print(trainset)
print(testset)
itorch.image(trainset.data[100]) -- display the 100-th image in dataset
print(classes[trainset.label[100]])
Out[2]:
Out[2]:
In [3]:
-- Functions that we need to use the data on the train
-- Function to obtain pairs x,y
setmetatable(trainset,
{__index = function(t, i)
return {t.data[i], t.label[i]}
end}
);
trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.
testset.data = testset.data:double()
-- Define size function over trainset
function trainset:size()
return self.data:size(1)
end
In [4]:
-- Rescale. The easy way
print(trainset.data[{{1},{1},{1,5},{1,5}}])
trainset.data:div(255.0)
print(trainset.data[{{1},{1},{1,5},{1,5}}])
testset.data:div(255.0)
Out[4]:
Out[4]:
In [5]:
require 'nn';
net = nn.Sequential()
net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 3 input image channels, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2)) -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5)) -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120)) -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(120, 84))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(84, 10)) -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax()) -- converts the output to a log-probability. Useful for classification problems
In [10]:
print('Net\n' .. net:__tostring());
Out[10]:
In [6]:
-- Criterions list
criterion = nn.ClassNLLCriterion()
In [7]:
-- Solver:
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
In [ ]:
input = trainset.data:cuda()
trainset.label = trainset.label:cuda()
for epoch = 1,5 do
net:zeroGradParameters()
output = net:forward(input)
output = joiner:forward(output):float()
gradOutputs = output:clone():zero()
ctcCost = cpu_ctc(output, gradOutputs, labels, sizes)
gradOutputs = gradOutputs:resize(nFrames, batchSize, nClasses):double()
gradOutputs = splitter:forward(gradOutputs)
gradInputs = net:backward(input, gradOutputs)
net:updateParameters(lrnRate)
end
Out[ ]:
In [8]:
require 'cunn';
net = net:cuda()
criterion = criterion:cuda()
trainset.data = trainset.data:cuda()
trainset.label = trainset.label:cuda()
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 5 -- just do 5 epochs of training.
trainer:train(trainset)
Out[8]:
Out[8]:
Out[8]:
Out[8]:
Out[8]:
Out[8]:
In [9]:
-- Predict one case
testset.data = testset.data:cuda()
testset.label = testset.label:cuda()
print(classes[testset.label[100]])
itorch.image(testset.data[100])
predicted = net:forward(testset.data[100])
print(predicted:exp())
for i=1,predicted:size(1) do
print(classes[i], predicted[i])
end
Out[9]:
Out[9]:
Out[9]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [12]:
require 'dp';
In [15]:
-- Load dataset and preprocess it
local input_preprocess = {}
table.insert(input_preprocess, dp.Standardize())
-- table.insert(input_preprocess, dp.ZCA())
-- table.insert(input_preprocess, dp.GCN())
-- table.insert(input_preprocess, dp.LeCunLCN{progress=true})
ds = dp.Cifar10{data_path = '/tmp', input_preprocess = input_preprocess}
print(ds)
print(ds:featureSize()) -- inputSize
Out[15]:
Out[15]:
Out[15]:
Out[15]:
Out[15]:
Out[15]:
In [16]:
print(ds:classes())
Out[16]:
In [ ]:
-- Define the model
require 'nn';
net = nn.Sequential()
net:add(nn.Convert(ds:ioShapes(), 'bf')) -- to batchSize x nFeature (also type converts)
net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 3 input image channels, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2)) -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5)) -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120)) -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(120, 84))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(84, 10)) -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax()) -- converts the output to a log-probability. Useful for classification problems
In [ ]:
-- PENDING TO FINISH
-- Define AdaptiveDecay
opt.maxWait = 4
opt.decayFactor = 0.001
ad = dp.AdaptiveDecay{max_wait = opt.maxWait, decay_factor=opt.decayFactor}
-- Define the optimizer
opt.batchSize = 32
--opt.accUpdate =
opt.learningRate = 0.1
opt.minLR = 0.00001
opt.momentum = 0
opt.maxOutNorm = 1
train = dp.Optimizer{
acc_update = opt.accUpdate,
loss = nn.ModuleCriterion(nn.ClassNLLCriterion(), nil, nn.Convert()),
epoch_callback = function(model, report) -- called every epoch
-- learning rate decay
if report.epoch > 0 then
if opt.lrDecay == 'adaptive' then
opt.learningRate = opt.learningRate*ad.decay
ad.decay = 1
end
opt.learningRate = math.max(opt.minLR, opt.learningRate)
print("learningRate", opt.learningRate)
end
end,
callback = function(model, report) -- called every batch
if opt.accUpdate then
model:accUpdateGradParameters(model.dpnn_input, model.output, opt.learningRate)
else
model:updateGradParameters(opt.momentum) -- affects gradParams
model:updateParameters(opt.learningRate) -- affects params
end
model:maxParamNorm(opt.maxOutNorm) -- affects params
model:zeroGradParameters() -- affects gradParams
end,
feedback = dp.Confusion(),
sampler = dp.ShuffleSampler{batch_size = opt.batchSize},
progress = opt.progress
}
In [ ]:
-- Define the evaluators
valid = dp.Evaluator{
feedback = dp.Confusion(),
sampler = dp.Sampler{batch_size = opt.batchSize}
}
test = dp.Evaluator{
feedback = dp.Confusion(),
sampler = dp.Sampler{batch_size = opt.batchSize}
}
In [ ]:
In [ ]:
In [1]:
require 'nn';
In [4]:
net = nn.Sequential()
net:add(nn.SpatialConvolution(1, 6, 5, 5)) -- 1 input image channel, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2)) -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5)) -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120)) -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(120, 84))
net:add(nn.ReLU()) -- non-linearity
net:add(nn.Linear(84, 10)) -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.SoftMax()) -- converts the output to a log-probability. Useful for classification problems
print('Lenet5\n' .. net:__tostring());
Out[4]:
In [7]:
input = torch.ones(1,32,32)
output = net:forward(input)
print(output)
net:zeroGradParameters()
gradInput = net:backward(input, torch.rand(10))
print(#gradInput)
Out[7]:
Out[7]:
In [ ]: